package earley;

/**
 * 
 * Item:
 * LEFT -> r.ight @ m
 * 
 * @author pavel
 *
 */
public class Item implements Comparable<Item>{
	final Rule rule;
	final int dotPos;
	final int symbolPos;
	
	public Item(Rule rule, int dotPos, int symbolPos) {
		this.rule = rule;
		this.dotPos = dotPos;
		this.symbolPos = symbolPos;
	}
	
	@Override
	protected Item clone() {
		return new Item(rule, dotPos, symbolPos);
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(rule.left.toString());
		sb.append(" -> ");
		int i = 0;
		for (; i < dotPos; i++) {
			sb.append(rule.right.get(i));
		}
		sb.append("*");
		for (; i < rule.right.size(); i++) {
			sb.append(rule.right.get(i));
		}
		sb.append("  @ " + symbolPos);
		return sb.toString();
	}
	
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Item)) {
			return false;
		}
		final Item other = (Item) obj;
		return other.toString().equals(this.toString());
	}

	@Override
	public int compareTo(Item other) {
		return other.toString().compareTo(this.toString());
	}
	
}
